using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TripTracker
{
    public partial class SettingsForm : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public SettingsForm()
        {
            InitializeComponent();
            GPSPortComboBox.SelectedItem = Settings.getSettings().GPSPort;
            WebTrackerCheckBox.Checked = Settings.getSettings().WebTrackerActivated;
            UserNameTextBox.Text = Settings.getSettings().WebTrackerUserName;
            PasswordTextBox.Text = Settings.getSettings().WebTrackerPassword;
            SecondsTextBox.Text = Settings.getSettings().WebTrackerSeconds.ToString();
            if (!WebTrackerCheckBox.Checked)
                panel1.Visible = false;
        }

        /// <summary>
        /// Event raised up whenever user clicks on the close button to discard changes
        /// on settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Event raised up whenever user clicks on the save button to save the settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void save_Click(object sender, EventArgs e)
        {
            try
            {
                int seconds = System.Convert.ToInt32(SecondsTextBox.Text);
                if(seconds > 60 || seconds < 10)
                {
                    MessageBox.Show("Please enter a value >= 10 and <= 60");

                } 
                else 
                {
                    Settings.getSettings().setSettings(GPSPortComboBox.SelectedItem.ToString(), WebTrackerCheckBox.Checked, UserNameTextBox.Text, PasswordTextBox.Text, System.Convert.ToInt32(SecondsTextBox.Text));
                }
                Close();
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Please select valid GPS COM-port.");
            }
            catch (FormatException)
            {
                MessageBox.Show("Not valid!");
            }

        }

        /// <summary>
        /// Activates/deactivates the webtracker functionality
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebTrackerCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            this.panel1.Visible = !this.panel1.Visible;
        }

        /// <summary>
        /// Calls a webservice to check the user credentials
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkConectionButton_Click(object sender, EventArgs e)
        {
            TripTrackerWebService.TripTrackerWebService TripTrackerWebService = new TripTracker.TripTrackerWebService.TripTrackerWebService();
            
            if (TripTrackerWebService.checkUser(this.UserNameTextBox.Text, this.PasswordTextBox.Text))
                MessageBox.Show("Account is valid!");
            else
                MessageBox.Show("Warning!\nAccount is not valid!");
        }

    }
}